package net.qiqbframework.messaging.task;

import net.qiqbframework.common.sorting.Priority;
import net.qiqbframework.messaging.Message;
import net.qiqbframework.messaging.annotation.HandlerParameterResolver;
import net.qiqbframework.messaging.annotation.HandlerParameterResolverFactory;

import java.lang.reflect.Executable;
import java.lang.reflect.Parameter;
@Priority(Priority.HIGHEST)
public class MessageTaskParameterResolverFactory implements HandlerParameterResolverFactory,HandlerParameterResolver<MessageTask<?>> {
    @Override
    public HandlerParameterResolver<?> createInstance(Executable executable, Parameter[] parameters, int parameterIndex) {
        if (MessageTask.class.equals(parameters[parameterIndex].getType())) {
            return this;
        }
        return null;
    }

    @Override
    public boolean support(Message<?> message) {
        return true;
    }

    @Override
    public MessageTask<?> resolveParameter(Message<?> message) {
        if (CurrentMessageTask.isStarted()){
            return CurrentMessageTask.get();
        }
        return null;
    }
}
